﻿using Microsoft.DotNet.PlatformAbstractions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using System.IO;
using System;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Swashbuckle.AspNetCore.Filters;
using Microsoft.AspNetCore.Builder;

namespace SwBasic
{
    /// <summary>
    /// Api接口版本 自定义
    /// </summary>
    public enum ApiVersions
    {
        /// <summary>
        /// v1 版本
        /// </summary>
        v1 = 1,
        /// <summary>
        /// v2 版本
        /// </summary>
        v2 = 2,
    }
    
    public static class SwaggerStartUp
    {
        public static string ApiName { get; set; } = "Blog.Core";
        public static void AddSwaggerStartUp(this IServiceCollection services)
        {
            //遍历出全部的版本，做文档信息展示

            var basePath = ApplicationEnvironment.ApplicationBasePath;
            // var ApiName = AppSettings.app(new string[] { });
            services.AddSwaggerGen(c =>
            {
                typeof(ApiVersions).GetEnumNames().ToList().ForEach(version =>
                {
                   
                    c.SwaggerDoc(version, new OpenApiInfo
                    {
                        // {ApiName} 定义成全局变量，方便修改
                        Version = version,
                        Title = $"{ApiName} 接口文档——{RuntimeInformation.FrameworkDescription}",
                        Description = $"{ApiName} HTTP API {version}",
                        Contact = new OpenApiContact { Name = ApiName, Email = "Blog.Core@xxx.com", Url = new Uri("https://www.jianshu.com/u/94102b59cc2a") },
                        License = new OpenApiLicense { Name = ApiName, Url = new Uri("https://www.jianshu.com/u/94102b59cc2a") }
                    });
                    c.OrderActionsBy(o => o.RelativePath);


                });
                var xmlPath = Path.Combine(basePath, "SwBasic.xml");
                c.IncludeXmlComments(xmlPath, true);//是否包含控制器的注释
                var xmlModelPath = Path.Combine(basePath, "SwBasic.Model.xml");
                c.IncludeXmlComments(xmlModelPath);

                c.OperationFilter<AddResponseHeadersFilter>();
                c.OperationFilter<AppendAuthorizeToSummaryOperationFilter>();
                c.OperationFilter<SecurityRequirementsOperationFilter>();

                c.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme()
                {
                    Description = "1111",
                    Name = "Authorization",
                    In = ParameterLocation.Header,
                    Type = SecuritySchemeType.ApiKey,
                });
            });

        }

        public static void UseSwaggerMildd(this IApplicationBuilder app)
        {
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                typeof(ApiVersions).GetEnumNames().ToList().ForEach(version =>
                {
                    c.SwaggerEndpoint($"/swagger/{version}/swagger.json", $"{ApiName} {version}");
                });
                //路径配置，设置为空，表示直接在根域名（localhost:8001）访问该文件,注意localhost:8001/swagger是访问不到的，去launchSettings.json把launchUrl去掉，如果你想换一个路径，直接写名字即可，比如直接写c.RoutePrefix = "doc";
                c.RoutePrefix = "";
            });
        }
    }
}
